package com.self.cgqcompose.ui.test

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.graphics.ClipOp
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import androidx.constraintlayout.compose.ConstraintLayout
import com.self.cgqcompose.ui.composables.CustomBoxWithOverflow

@Preview(showBackground = true, showSystemUi = true, backgroundColor = 0xfffffff0)
@Composable
fun RowTest(){
//    val list = listOf(1,2,3,4,5,6,7,8)
//    list.forEach {
//        println(it)
//    }
//    Row(modifier = Modifier.fillMaxWidth().height(50.dp).padding(top = 30.dp)) {
//        list.forEach {
//            Box(modifier = Modifier.weight(1f).height(80.dp).zIndex(1f).offset(y = (-15).dp)){
//                Text(text = it.toString())
//            }
//        }
//    }
    val list = listOf("A", "B", "C")


    Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
//        Box(modifier = Modifier
//            .height(50.dp)
//            .padding(top = 0.dp)
//            .graphicsLayer(clip = false), contentAlignment = Alignment.BottomCenter) {
//            // 这个是 Row 的背景层，固定高度 50.dp
////        Row(
////            modifier = Modifier
////                .fillMaxWidth()
////                .height(50.dp)
////                .background(Color.LightGray)
////        ) {
////            repeat(list.size) {
////                Spacer(modifier = Modifier.weight(1f))
////            }
////        }
//
//            // 这个是真正的内容层，高度 80.dp，不受 Row 限制
//            Row(
//                modifier = Modifier
//                    .fillMaxWidth()
//                    .padding(top = 0.dp), // 可调节
//                verticalAlignment = Alignment.Bottom,
//                horizontalArrangement = Arrangement.SpaceEvenly
//            ) {
//                list.forEach {
//                    Box(
//                        modifier = Modifier
//                            .weight(1f)
//                            .height(if(it == "B") 80.dp else if (it == "C") 50.dp else 50.dp) // 真正生效
//                            .background(Color.Cyan),
//                        contentAlignment = Alignment.Center
//                    ) {
//                        Text(text = it)
//                    }
//                }
//            }
        CustomBoxWithOverflow(modifier = Modifier.fillMaxWidth()) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 0.dp), // 可调节
                verticalAlignment = Alignment.Bottom,
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                list.forEach {
                    Box(
                        modifier = Modifier
                            .weight(1f)
                            .height(if(it == "B") 80.dp else if (it == "C") 50.dp else 50.dp) // 真正生效
                            .background(Color.Cyan),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(text = it)
                    }
                }
            }
        }
        }



}

@Preview(showBackground = true, showSystemUi = true, backgroundColor = 0xffffffff)
@Composable
fun ConstraintLayoutTest(){
    ConstraintLayout(
        modifier = Modifier
            .fillMaxWidth()
            .height(50.dp)
            .background(Color.LightGray)
//            .graphicsLayer(clip = false)
    ) {
        // 创建引用
        val (box1, box2, text) = createRefs()

        Box(
            modifier = Modifier
                .size(100.dp)
                .background(Color.Red)
                .zIndex(10f)
                .constrainAs(box1) {
                    top.linkTo(parent.top, margin = 16.dp)
                    start.linkTo(parent.start, margin = 16.dp)
                }
        )

        Box(
            modifier = Modifier
                .size(100.dp)
                .background(Color.Green)
                .constrainAs(box2) {
                    top.linkTo(box1.bottom, margin = 16.dp)
                    start.linkTo(box1.end, margin = 16.dp)
                }
        )

        Text(
            text = "Hello ConstraintLayout",
            modifier = Modifier.constrainAs(text) {
                top.linkTo(box2.bottom, margin = 16.dp)
                start.linkTo(parent.start, margin = 16.dp)
            }
        )
    }
}